package cn.e2win.biz.tools;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import cn.e2win.biz.util.GenericsUtils;
import cn.e2win.biz.util.StringUtils;
public class CodeGenerator {


	public static String module = "crm";
	public static String entity = "customer";
	public static Class clazz = null;//ProdEndOrderItems.class;
	public static void main(String[] args) {
		try {
			propertyList = GenericsUtils.getDBFieldList(clazz);
			initReplaceMap2();
			initReplaceMap1();
//			String dstPath = destinationPathJava + module +"\\" + StringUtils.firstChar2UpperCase(entity) + "Controller.java";
//			File file = new File(dstPath);			
			CodeGenerator codeGenerator = new CodeGenerator();
			codeGenerator.generatCode("Dao");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	

	public void generatCode(String type) throws IOException{
		if("JSP".equals(type)){
			generatJsp("List");
			generatJsp("Edit");
		}else if("JAVA".equals(type)){
			generatJava("Controller");
			generatJava("Service");
			generatJava("Dao");
			generatJava("DaoImpl");	
		}else if (type == null){
			generatList();
			generatEdit();
			generatJava("Controller");
			generatJava("Service");
			generatJava("Dao");
			generatJava("DaoImpl");
		}else{
			generatJava(type);
			if("Dao".equals(type)){
				generatJava("DaoImpl");				
			}
		}
	}
	
	

	public static List<Field> propertyList;
	public static Map<String, String> replaceMap1;
	public static Map<String, String> replaceMap2;
	
	public static final String templatePath = "C:\\Users\\duq\\workspace\\jxdq\\WebContent\\WEB-INF\\view\\template\\";
	public static final String destinationPathJava = "C:\\Users\\duq\\workspace\\jxdq\\src\\com\\dms\\";
	public static final String destinationPathDao = "C:\\Users\\duq\\workspace\\jxdq\\src\\com\\mvc\\entity\\";
	public static final String destinationPathJsp = "C:\\Users\\duq\\workspace\\jxdq\\WebContent\\WEB-INF\\view\\";
	
	public static final String LIST_SC_DIV_TPL =  Matcher.quoteReplacement("<!-- LIST_SC_DIV_TPL -->");
	public static final String LIST_TH_DIV_TPL =  Matcher.quoteReplacement("<!-- LIST_TH_DIV_TPL -->");
	public static final String LIST_TB_DIV_TPL =  Matcher.quoteReplacement("<!-- LIST_TB_DIV_TPL -->");

	public static final String LIST_SC_TPL =  Matcher.quoteReplacement("\r\n					PROPERTY:<input type=\"text\" name=\"map.PROPERTY\" value=\"${pager.scMap.PROPERTY}\">");
	public static final String LIST_TH_TPL =  Matcher.quoteReplacement("\r\n				<th>PROPERTY</th>");
	public static final String LIST_TB_TPL =  Matcher.quoteReplacement("\r\n					<td>${item.PROPERTY}</td>");
	

	public static final String EDIT_P_DIV_TPL =  Matcher.quoteReplacement("<!-- EDIT_P_DIV_TPL -->");
	
	public static final String EDIT_P_TPL =  Matcher.quoteReplacement(
			"\r\n			<p><label>PROPERTY：</label>" +
			"\r\n			    <input id=\"PROPERTY\" type=\"text\" size=\"30\" name=\"PROPERTY\" value=\"${object.PROPERTY}\" ></input>" +
			"\r\n			</p>");
	

	
	
	public static void initReplaceMap1(){
		replaceMap1 = new HashMap<String, String>();
		replaceMap1.put(LIST_SC_DIV_TPL, replaceMap2.get("LIST_SC_TPL"));
		replaceMap1.put(LIST_TH_DIV_TPL, replaceMap2.get("LIST_TH_TPL"));
		replaceMap1.put(LIST_TB_DIV_TPL, replaceMap2.get("LIST_TB_TPL"));

		replaceMap1.put(EDIT_P_DIV_TPL, replaceMap2.get("EDIT_P_TPL"));
	}
	
	public static void initReplaceMap2(){
		replaceMap2 = new HashMap<String, String>();
		String scTpl="";
		String thTpl="";
		String tbTpl="";
		
		String pTpl="";
		String proerty="";
		for(Field field : propertyList){
			proerty = field.getName();
			if(!"id".equals(proerty)){
				scTpl = scTpl + LIST_SC_TPL.replaceAll("PROPERTY", proerty);
				thTpl = thTpl + LIST_TH_TPL.replaceAll("PROPERTY", proerty);
				tbTpl = tbTpl + LIST_TB_TPL.replaceAll("PROPERTY", proerty);
	
				pTpl = pTpl + EDIT_P_TPL.replaceAll("PROPERTY", proerty);
			}
		}		
		replaceMap2.put("LIST_SC_TPL", scTpl);
		replaceMap2.put("LIST_TH_TPL", thTpl);
		replaceMap2.put("LIST_TB_TPL", tbTpl);	
		

		replaceMap2.put("EDIT_P_TPL", pTpl);	
	}
	
	FileHelper fileHelper = new FileHelper();
	private void generatJsp(String type) throws IOException{
		if("List".equals(type)){
			generatList();
		}else if ("Edit".equals(type)) {
			generatEdit();
		}
		
	}
	
	private void generatList() throws IOException{
		System.err.println("\n\n开始生成List文件............");
		String tplPath = templatePath + "list.jsp.txt";
		String dstPath = destinationPathJsp + module + "\\" + entity + "List.jsp";
		String fileContent = fileHelper.reader(tplPath);
		fileContent = fileContent.replaceAll(LIST_SC_DIV_TPL,replaceMap1.get(LIST_SC_DIV_TPL));
		fileContent = fileContent.replaceAll(LIST_TH_DIV_TPL,replaceMap1.get(LIST_TH_DIV_TPL));
		fileContent = fileContent.replaceAll(LIST_TB_DIV_TPL,replaceMap1.get(LIST_TB_DIV_TPL));
		fileHelper.writer(fileContent, dstPath);
		System.err.println("成功生成List文件" + dstPath);
		
	}	
	
	private void generatEdit() throws IOException{
		System.err.println("\n\n开始生成Edit文件............");
		String tplPath = templatePath + "edit.jsp.txt";
		String dstPath = destinationPathJsp + module + "\\" + entity + "Edit.jsp";
		String fileContent = fileHelper.reader(tplPath);
		fileContent = fileContent.replaceAll("ENTITY_NAME",entity);
		fileContent = fileContent.replaceAll(EDIT_P_DIV_TPL,replaceMap1.get(EDIT_P_DIV_TPL));

		fileHelper.writer(fileContent, dstPath);
		System.err.println("成功生成List文件" + dstPath);
	}
	
	private void generatJava(String type)throws IOException{
		System.err.println("\n\n开始生成" + type + "文件............");
		String upperEntity= StringUtils.firstChar2UpperCase(entity);
		String className = clazz.getSimpleName();
		String lowerClassName = StringUtils.firstChar2LowerCase(className);
		String tplPath = templatePath + type + ".java.txt";
		String dstPath = destinationPathJava + module + "\\"  + upperEntity + type + ".java";
		if("Dao".equals(type) || "DaoImpl".equals(type)){
			dstPath = destinationPathDao + className + type + ".java";
		}
		String fileContent = fileHelper.reader(tplPath);
		fileContent = fileContent.replaceAll("mODULE_NAME", module);
		fileContent = fileContent.replaceAll("eNTITY_NAME", entity);
		fileContent = fileContent.replaceAll("ENTITY_NAME", upperEntity);
		fileContent = fileContent.replaceAll("cLASS_NAME", lowerClassName);
		fileContent = fileContent.replaceAll("CLASS_NAME", className);
		fileHelper.writer(fileContent, dstPath);
		System.err.println("成功生成List文件" + dstPath);
	}
	
	
	
	
	
	
	
	
}
